#include "chess_board.h"
#include "game.h"



ChessBoard::ChessBoard()
{
	_coor = Coor::Create();
	_coor->SetPosition(棋盘偏移);

	_txtChuhe = g_Res->GetTextBoardCenter();
	_txtChuhe->GetCoor()->SetParent(_coor);
	_txtChuhe->SetString("楚河");
	_txtHanjie = g_Res->GetTextBoardCenter();
	_txtHanjie->GetCoor()->SetParent(_coor);
	_txtHanjie->SetString("漢界");

	//_txtChuhe->GetCoor()->SetRotate(Math::PI());
	_txtChuhe->SetPositionAlign(Xy2Pos(2, 4) + Position{ 0, 格子宽度 * 0.5f }, Align::CC);
	_txtHanjie->SetPositionAlign(Xy2Pos(6, 4) + Position{ 0, 格子宽度 * 0.5f }, Align::CC);
}

ChessBoard::~ChessBoard()
{
	delete _coor;
}

void ChessBoard::Render()
{
	g_gfx->SetCoor(_coor);
	g_gfx->SetColor(createColor(格子颜色));
	g_gfx->SetOrder(ORDER_BG);
	g_gfx->RectangleSolid2D(
		格子大小 * -0.5f, 
		格子大小 * -0.5f + toPosition(棋盘线条数) * 格子宽度);

	g_gfx->SetColor(createColor(线条颜色));
	g_gfx->SetOrder(ORDER_CHESS_GRID);
	// 横线
	for (size_t i = 0; i < 棋盘线条数[1]; ++i)
	{
		Position p;
		p[0] = 0;
		p[1] = i * 格子宽度;
		g_gfx->StrokeLine(p, p + Position{ 格子宽度 * 8, 0 }, 线条宽度);
	}

	// 竖线
	for (size_t i = 0; i < 棋盘线条数[0]; ++i)
	{
		if (i == 0 || i == 8)
		{
			Position p;
			p[0] = i * 格子宽度;
			p[1] = 0;
			g_gfx->StrokeLine(p, p + Position{ 0, 格子宽度 * 9 }, 线条宽度);
		}
		else
		{
			Position p;
			p[0] = i * 格子宽度;
			p[1] = 0;
			g_gfx->StrokeLine(p, p + Position{ 0, 格子宽度 * 4 }, 线条宽度);

			p[0] = i * 格子宽度;
			p[1] = 格子宽度 * 5;
			g_gfx->StrokeLine(p, p + Position{ 0, 格子宽度 * 4 }, 线条宽度);

		}
	}
	// 宫线
	g_gfx->StrokeLine(Xy2Pos(3, 0), Xy2Pos(5, 2), 线条宽度);
	g_gfx->StrokeLine(Xy2Pos(5, 0), Xy2Pos(3, 2), 线条宽度);
	g_gfx->StrokeLine(Xy2Pos(3, 7), Xy2Pos(5, 9), 线条宽度);
	g_gfx->StrokeLine(Xy2Pos(5, 7), Xy2Pos(3, 9), 线条宽度);

	// 炮位
	_render_loc({ 1, 2 });
	_render_loc({ 7, 2 });
	_render_loc({ 1, 7 });
	_render_loc({ 7, 7 });
	// 兵位
	for (int i = 0; i < 5; ++i)
	{
		int x = i * 2;
		_render_loc({ x, 3 }, i != 0, i != 4);
		_render_loc({ x, 6 }, i != 0, i != 4);
	}

	_txtChuhe->Render();
	_txtHanjie->Render();


	for (ChessMan& man : _vecMan)
	{
		man.Render();
	}
}

void ChessBoard::New()
{
	SetMan({ 0,9 }, ChessManType::红车);
	SetMan({ 1,9 }, ChessManType::红马);
	SetMan({ 2,9 }, ChessManType::红相);
	SetMan({ 3,9 }, ChessManType::红仕);
	SetMan({ 4,9 }, ChessManType::红帅);
	SetMan({ 5,9 }, ChessManType::红仕);
	SetMan({ 6,9 }, ChessManType::红相);
	SetMan({ 7,9 }, ChessManType::红马);
	SetMan({ 8,9 }, ChessManType::红车);
	SetMan({ 1,7 }, ChessManType::红炮);
	SetMan({ 7,7 }, ChessManType::红炮);
	SetMan({ 0,6 }, ChessManType::红兵);
	SetMan({ 2,6 }, ChessManType::红兵);
	SetMan({ 4,6 }, ChessManType::红兵);
	SetMan({ 6,6 }, ChessManType::红兵);
	SetMan({ 8,6 }, ChessManType::红兵);

	SetMan({ 0,0 }, ChessManType::黑车);
	SetMan({ 1,0 }, ChessManType::黑马);
	SetMan({ 2,0 }, ChessManType::黑象);
	SetMan({ 3,0 }, ChessManType::黑士);
	SetMan({ 4,0 }, ChessManType::黑将);
	SetMan({ 5,0 }, ChessManType::黑士);
	SetMan({ 6,0 }, ChessManType::黑象);
	SetMan({ 7,0 }, ChessManType::黑马);
	SetMan({ 8,0 }, ChessManType::黑车);
	SetMan({ 1,2 }, ChessManType::黑炮);
	SetMan({ 7,2 }, ChessManType::黑炮);
	SetMan({ 0,3 }, ChessManType::黑卒);
	SetMan({ 2,3 }, ChessManType::黑卒);
	SetMan({ 4,3 }, ChessManType::黑卒);
	SetMan({ 6,3 }, ChessManType::黑卒);
	SetMan({ 8,3 }, ChessManType::黑卒);
}

void ChessBoard::_render_loc(const Point& xy, bool left /*= true*/, bool right /*= true*/)
{
	Position pos;
	if (left)
	{
		// lt
		pos = Xy2Pos(xy);
		pos += { -位线间隔, -位线间隔 };
		g_gfx->StrokeLine(pos, pos - Position{ 位线长度, 0 }, 位线宽度);
		g_gfx->StrokeLine(pos, pos - Position{ 0, 位线长度 }, 位线宽度);
		// lb
		pos = Xy2Pos(xy);
		pos += { -位线间隔, 位线间隔 };
		g_gfx->StrokeLine(pos, pos - Position{ 位线长度, 0 }, 位线宽度);
		g_gfx->StrokeLine(pos, pos + Position{ 0, 位线长度 }, 位线宽度);
	}
	if (right)
	{
		// rt
		pos = Xy2Pos(xy);
		pos += { 位线间隔, -位线间隔 };
		g_gfx->StrokeLine(pos, pos + Position{ 位线长度, 0 }, 位线宽度);
		g_gfx->StrokeLine(pos, pos - Position{ 0, 位线长度 }, 位线宽度);
		// rb
		pos = Xy2Pos(xy);
		pos += { 位线间隔, 位线间隔 };
		g_gfx->StrokeLine(pos, pos + Position{ 位线长度, 0 }, 位线宽度);
		g_gfx->StrokeLine(pos, pos + Position{ 0, 位线长度 }, 位线宽度);
	}
}
